# author: Jochen Rehmert, University of Basel
# journal: Journal of Politics
# article: Intra-party competition, geographic responsiveness 
#           and incumbent deselection in closed-list PR
# date: 12th August 2024
# content: Script to replicate Figures 10 and 11 in the Appendix
# needs data: "cj_2.RDS"
# see also: "data_description_cj_2.txt"

# directory
setwd("")

sink("log_rep_figure10_11.txt")
cat("start logging\n")
dat <- readRDS("cj_2.RDS")


##############################
# Data preparation
##############################

dat$party <- as.character(NA)
dat$party[dat$Q1_1 == 1] <- "Greens"
dat$party[dat$Q1_1 == 431] <- "CDU"
dat$party[dat$Q1_1 == 876] <- "FDP"
dat$party[dat$Q1_1 == 1322] <- "LINKE"
dat$party[dat$Q1_1 == 1680] <- "SPD" 

# statements on delegates' knowledge
dat$informed_MdB_KV <- as.numeric(NA)
dat$informed_MdB_KV[dat$E3_1 == 1] <- 0 # doesnt agree
dat$informed_MdB_KV[dat$E3_1 == 2] <- 1 
dat$informed_MdB_KV[dat$E3_1 == 3] <- 2 
dat$informed_MdB_KV[dat$E3_1 == 4] <- 3 
dat$informed_MdB_KV[dat$E3_1 == 5] <- 4 
dat$informed_MdB_KV[dat$E3_1 == 6] <- 5 
dat$informed_MdB_KV[dat$E3_1 == 7] <- 6 # agrees

dat$informed_MdB_LV <- as.numeric(NA)
dat$informed_MdB_LV[dat$E3_2 == 1] <- 0 # doesnt agree
dat$informed_MdB_LV[dat$E3_2 == 2] <- 1 
dat$informed_MdB_LV[dat$E3_2 == 3] <- 2 
dat$informed_MdB_LV[dat$E3_2 == 4] <- 3 
dat$informed_MdB_LV[dat$E3_2 == 5] <- 4 
dat$informed_MdB_LV[dat$E3_2 == 6] <- 5 
dat$informed_MdB_LV[dat$E3_2 == 7] <- 6 # agrees

dat$informed_MdB_pty <- as.numeric(NA)
dat$informed_MdB_pty[dat$E3_3 == 1] <- 0 # doesnt agree
dat$informed_MdB_pty[dat$E3_3 == 2] <- 1 
dat$informed_MdB_pty[dat$E3_3 == 3] <- 2 
dat$informed_MdB_pty[dat$E3_3 == 4] <- 3 
dat$informed_MdB_pty[dat$E3_3 == 5] <- 4 
dat$informed_MdB_pty[dat$E3_3 == 6] <- 5 
dat$informed_MdB_pty[dat$E3_3 == 7] <- 6 # agrees

dat$informed_dailypolitics <- as.numeric(NA)
dat$informed_dailypolitics[dat$E3_4 == 1] <- 0 # doesnt agree
dat$informed_dailypolitics[dat$E3_4 == 2] <- 1 
dat$informed_dailypolitics[dat$E3_4 == 3] <- 2 
dat$informed_dailypolitics[dat$E3_4 == 4] <- 3 
dat$informed_dailypolitics[dat$E3_4 == 5] <- 4 
dat$informed_dailypolitics[dat$E3_4 == 6] <- 5 
dat$informed_dailypolitics[dat$E3_4 == 7] <- 6 # agrees

# delegates follow ...
dat$follows_own <- as.numeric(NA)
dat$follows_own[dat$E4_1 == 1] <- 0 # doesnt agree
dat$follows_own[dat$E4_1 == 2] <- 1 
dat$follows_own[dat$E4_1 == 3] <- 2 
dat$follows_own[dat$E4_1 == 4] <- 3 
dat$follows_own[dat$E4_1 == 5] <- 4 
dat$follows_own[dat$E4_1 == 6] <- 5 
dat$follows_own[dat$E4_1 == 7] <- 6 # agrees

dat$follows_KV <- as.numeric(NA)
dat$follows_KV[dat$E4_2 == 1] <- 0 # doesnt agree
dat$follows_KV[dat$E4_2 == 2] <- 1 
dat$follows_KV[dat$E4_2 == 3] <- 2 
dat$follows_KV[dat$E4_2 == 4] <- 3 
dat$follows_KV[dat$E4_2 == 5] <- 4 
dat$follows_KV[dat$E4_2 == 6] <- 5 
dat$follows_KV[dat$E4_2 == 7] <- 6 # agrees

dat$follows_LV <- as.numeric(NA)
dat$follows_LV[dat$E4_3 == 1] <- 0 # doesnt agree
dat$follows_LV[dat$E4_3 == 2] <- 1 
dat$follows_LV[dat$E4_3 == 3] <- 2 
dat$follows_LV[dat$E4_3 == 4] <- 3 
dat$follows_LV[dat$E4_3 == 5] <- 4 
dat$follows_LV[dat$E4_3 == 6] <- 5 
dat$follows_LV[dat$E4_3 == 7] <- 6 # agrees

dat$follows_federal <- as.numeric(NA)
dat$follows_federal[dat$E4_4 == 1] <- 0 # doesnt agree
dat$follows_federal[dat$E4_4 == 2] <- 1 
dat$follows_federal[dat$E4_4 == 3] <- 2 
dat$follows_federal[dat$E4_4 == 4] <- 3 
dat$follows_federal[dat$E4_4 == 5] <- 4 
dat$follows_federal[dat$E4_4 == 6] <- 5 
dat$follows_federal[dat$E4_4 == 7] <- 6 # agrees

##############################
# Figure 10 in Appendix: Plot informed
##############################


plotDat <- as.data.frame(rbind(
  cbind(party = "B90/Gr", level = names(table(dat$informed_MdB_KV[dat$party == "Greens"])), value = table(dat$informed_MdB_KV[dat$party == "Greens"])/length(dat$informed_MdB_KV[dat$party == "Greens"]), indicator = "Local Chapter Incumbents"),
  cbind(party = "B90/Gr", level = names(table(dat$informed_MdB_LV[dat$party == "Greens"])),value = table(dat$informed_MdB_LV[dat$party == "Greens"])/length(dat$informed_MdB_LV[dat$party == "Greens"]), indicator = "Regional Chapter Incumbents"),
  cbind(party = "B90/Gr", level = names(table(dat$informed_MdB_pty[dat$party == "Greens"])),value = table(dat$informed_MdB_pty[dat$party == "Greens"])/length(dat$informed_MdB_pty[dat$party == "Greens"]), indicator = "Federal Party Incumbents"),
  cbind(party = "B90/Gr", level = names(table(dat$informed_dailypolitics[dat$party == "Greens"])),value = table(dat$informed_dailypolitics[dat$party == "Greens"])/length(dat$informed_dailypolitics[dat$party == "Greens"]), indicator = "Daily Political Issues"),
  cbind(party = "CDU", level = names(table(dat$informed_MdB_KV[dat$party == "CDU"])),value = table(dat$informed_MdB_KV[dat$party == "CDU"])/length(dat$informed_MdB_KV[dat$party == "CDU"]), indicator = "Local Chapter Incumbents"),
  cbind(party = "CDU", level = names(table(dat$informed_MdB_LV[dat$party == "CDU"])),value = table(dat$informed_MdB_LV[dat$party == "CDU"])/length(dat$informed_MdB_LV[dat$party == "CDU"]), indicator = "Regional Chapter Incumbents"),
  cbind(party = "CDU", level = names(table(dat$informed_MdB_pty[dat$party == "CDU"])),value = table(dat$informed_MdB_pty[dat$party == "CDU"])/length(dat$informed_MdB_pty[dat$party == "CDU"]), indicator = "Federal Party Incumbents"),
  cbind(party = "CDU", level = names(table(dat$informed_dailypolitics[dat$party == "CDU"])),value = table(dat$informed_dailypolitics[dat$party == "CDU"])/length(dat$informed_dailypolitics[dat$party == "CDU"]), indicator = "Daily Political Issues"),
  cbind(party = "FDP", level = names(table(dat$informed_MdB_KV[dat$party == "FDP"])),value = table(dat$informed_MdB_KV[dat$party == "FDP"])/length(dat$informed_MdB_KV[dat$party == "FDP"]), indicator = "Local Chapter Incumbents"),
  cbind(party = "FDP", level = names(table(dat$informed_MdB_LV[dat$party == "FDP"])),value = table(dat$informed_MdB_LV[dat$party == "FDP"])/length(dat$informed_MdB_LV[dat$party == "FDP"]), indicator = "Regional Chapter Incumbents"),
  cbind(party = "FDP", level = names(table(dat$informed_MdB_pty[dat$party == "FDP"])),value = table(dat$informed_MdB_pty[dat$party == "FDP"])/length(dat$informed_MdB_pty[dat$party == "FDP"]), indicator = "Federal Party Incumbents"),
  cbind(party = "FDP", level = names(table(dat$informed_dailypolitics[dat$party == "FDP"])),value = table(dat$informed_dailypolitics[dat$party == "FDP"])/length(dat$informed_dailypolitics[dat$party == "FDP"]), indicator = "Daily Political Issues"),
  cbind(party = "SPD", level = names(table(dat$informed_MdB_KV[dat$party == "SPD"])),value = table(dat$informed_MdB_KV[dat$party == "SPD"])/length(dat$informed_MdB_KV[dat$party == "SPD"]), indicator = "Local Chapter Incumbents"),
  cbind(party = "SPD", level = names(table(dat$informed_MdB_LV[dat$party == "SPD"])),value = table(dat$informed_MdB_LV[dat$party == "SPD"])/length(dat$informed_MdB_LV[dat$party == "SPD"]), indicator = "Regional Chapter Incumbents"),
  cbind(party = "SPD", level = names(table(dat$informed_MdB_pty[dat$party == "SPD"])),value = table(dat$informed_MdB_pty[dat$party == "SPD"])/length(dat$informed_MdB_pty[dat$party == "SPD"]), indicator = "Federal Party Incumbents"),
  cbind(party = "SPD", level = names(table(dat$informed_dailypolitics[dat$party == "SPD"])),value = table(dat$informed_dailypolitics[dat$party == "SPD"])/length(dat$informed_dailypolitics[dat$party == "SPD"]), indicator = "Daily Political Issues"),
  cbind(party = "LINKE", level = names(table(dat$informed_MdB_KV[dat$party == "LINKE"])),value = table(dat$informed_MdB_KV[dat$party == "LINKE"])/length(dat$informed_MdB_KV[dat$party == "LINKE"]), indicator = "Local Chapter Incumbents"),
  cbind(party = "LINKE", level = names(table(dat$informed_MdB_LV[dat$party == "LINKE"])),value = table(dat$informed_MdB_LV[dat$party == "LINKE"])/length(dat$informed_MdB_LV[dat$party == "LINKE"]), indicator = "Regional Chapter Incumbents"),
  cbind(party = "LINKE", level = names(table(dat$informed_MdB_pty[dat$party == "LINKE"])),value = table(dat$informed_MdB_pty[dat$party == "LINKE"])/length(dat$informed_MdB_pty[dat$party == "LINKE"]), indicator = "Federal Party Incumbents"),
  cbind(party = "LINKE", level = names(table(dat$informed_dailypolitics[dat$party == "LINKE"])),value = table(dat$informed_dailypolitics[dat$party == "LINKE"])/length(dat$informed_dailypolitics[dat$party == "LINKE"]), indicator = "Daily Political Issues")
))

plotDat$value <- as.numeric(as.character(plotDat$value))
plotDat$level <- as.numeric(as.character(plotDat$level))

plot.tmp <- data.frame(rbind(cbind(party = "CDU", level = 0, value = 0, indicator = "Local Chapter Incumbents"),
                             cbind(party = "CDU", level = 0, value = 0, indicator = "Daily Political Issues"),
                             cbind(party = "FDP", level = 0, value = 0, indicator = "Local Chapter Incumbents"),
                             cbind(party = "FDP", level = 6, value = 0, indicator = "Federal Party Incumbents"),
                             cbind(party = "FDP", level = 0, value = 0, indicator = "Daily Political Issues")))
plot.tmp$level <- as.numeric(as.character(plot.tmp$level))
plot.tmp$value <- as.numeric(as.character(plot.tmp$value))

plotDat <- rbind(plotDat, plot.tmp)



p1 <- ggplot(plotDat[plotDat$indicator != "Regional Chapter Incumbents",])
p1 = p1 + geom_col(aes(level, value)) + ylab("Relative Frequency")
p1 = p1 + facet_grid(party ~ indicator) + xlab("")
p1 = p1 + scale_x_continuous(breaks = c(0:6),labels=c("Strongly\nDisagree", "", "", "", "","","Strongly\nAgree"))
p1 = p1 + theme(strip.text.x = element_text(size = 14),
                    strip.background =element_rect(fill="white"),
                    axis.text.x = element_text(face="bold", size=8, colour = "black"),
                    axis.title.y = element_text(face="bold", size=8))
cat("create Figure 10\n")
p1


##############################
# Figure 11 in Appendix: Plot vote recommendations
##############################


plotDat <- as.data.frame(rbind(
  cbind(party = "B90/Gr", level = names(table(dat$follows_own[dat$party == "Greens"])), value = table(dat$follows_own[dat$party == "Greens"])/length(dat$follows_own[dat$party == "Greens"]), indicator = "Own Opinion"),
  cbind(party = "B90/Gr", level = names(table(dat$follows_KV[dat$party == "Greens"])),value = table(dat$follows_KV[dat$party == "Greens"])/length(dat$follows_KV[dat$party == "Greens"]), indicator = "Local Chapter"),
  cbind(party = "B90/Gr", level = names(table(dat$follows_LV[dat$party == "Greens"])),value = table(dat$follows_LV[dat$party == "Greens"])/length(dat$follows_LV[dat$party == "Greens"]), indicator = "Regional Chapter"),
  cbind(party = "B90/Gr", level = names(table(dat$follows_federal[dat$party == "Greens"])),value = table(dat$follows_federal[dat$party == "Greens"])/length(dat$follows_federal[dat$party == "Greens"]), indicator = "Federal Party"),
  cbind(party = "CDU", level = names(table(dat$follows_own[dat$party == "CDU"])),value = table(dat$follows_own[dat$party == "CDU"])/length(dat$follows_own[dat$party == "CDU"]), indicator = "Own Opinion"),
  cbind(party = "CDU", level = names(table(dat$follows_LV[dat$party == "CDU"])),value = table(dat$follows_LV[dat$party == "CDU"])/length(dat$follows_LV[dat$party == "CDU"]), indicator = "Regional Chapter"),
  cbind(party = "CDU", level = names(table(dat$follows_federal[dat$party == "CDU"])),value = table(dat$follows_federal[dat$party == "CDU"])/length(dat$follows_federal[dat$party == "CDU"]), indicator = "Federal Party"),
  cbind(party = "CDU", level = names(table(dat$follows_KV[dat$party == "CDU"])),value = table(dat$follows_KV[dat$party == "CDU"])/length(dat$follows_KV[dat$party == "CDU"]), indicator = "Local Chapter"),
  cbind(party = "FDP", level = names(table(dat$follows_own[dat$party == "FDP"])),value = table(dat$follows_own[dat$party == "FDP"])/length(dat$follows_own[dat$party == "FDP"]), indicator = "Own Opinion"),
  cbind(party = "FDP", level = names(table(dat$follows_LV[dat$party == "FDP"])),value = table(dat$follows_LV[dat$party == "FDP"])/length(dat$follows_LV[dat$party == "FDP"]), indicator = "Regional Chapter"),
  cbind(party = "FDP", level = names(table(dat$follows_federal[dat$party == "FDP"])),value = table(dat$follows_federal[dat$party == "FDP"])/length(dat$follows_federal[dat$party == "FDP"]), indicator = "Federal Party"),
  cbind(party = "FDP", level = names(table(dat$follows_KV[dat$party == "FDP"])),value = table(dat$follows_KV[dat$party == "FDP"])/length(dat$follows_KV[dat$party == "FDP"]), indicator = "Local Chapter"),
  cbind(party = "SPD", level = names(table(dat$follows_own[dat$party == "SPD"])),value = table(dat$follows_own[dat$party == "SPD"])/length(dat$follows_own[dat$party == "SPD"]), indicator = "Own Opinion"),
  cbind(party = "SPD", level = names(table(dat$follows_LV[dat$party == "SPD"])),value = table(dat$follows_LV[dat$party == "SPD"])/length(dat$follows_LV[dat$party == "SPD"]), indicator = "Regional Chapter"),
  cbind(party = "SPD", level = names(table(dat$follows_federal[dat$party == "SPD"])),value = table(dat$follows_federal[dat$party == "SPD"])/length(dat$follows_federal[dat$party == "SPD"]), indicator = "Federal Party"),
  cbind(party = "SPD", level = names(table(dat$follows_KV[dat$party == "SPD"])),value = table(dat$follows_KV[dat$party == "SPD"])/length(dat$follows_KV[dat$party == "SPD"]), indicator = "Local Chapter"),
  cbind(party = "LINKE", level = names(table(dat$follows_own[dat$party == "LINKE"])),value = table(dat$follows_own[dat$party == "LINKE"])/length(dat$follows_own[dat$party == "LINKE"]), indicator = "Own Opinion"),
  cbind(party = "LINKE", level = names(table(dat$follows_LV[dat$party == "LINKE"])),value = table(dat$follows_LV[dat$party == "LINKE"])/length(dat$follows_LV[dat$party == "LINKE"]), indicator = "Regional Chapter"),
  cbind(party = "LINKE", level = names(table(dat$follows_federal[dat$party == "LINKE"])),value = table(dat$follows_federal[dat$party == "LINKE"])/length(dat$follows_federal[dat$party == "LINKE"]), indicator = "Federal Party"),
  cbind(party = "LINKE", level = names(table(dat$follows_KV[dat$party == "LINKE"])),value = table(dat$follows_KV[dat$party == "LINKE"])/length(dat$follows_KV[dat$party == "LINKE"]), indicator = "Local Chapter")
))

plotDat$value <- as.numeric(as.character(plotDat$value))
plotDat$level <- as.numeric(as.character(plotDat$level))



p2 <- ggplot(plotDat[plotDat$indicator != "Regional Chapter",])
p2 = p2 + geom_col(aes(level, value)) + ylab("Relative Frequency")
p2 = p2 + facet_grid(party ~ indicator) + xlab("")
p2 = p2 + scale_x_continuous(breaks = c(0:6),labels=c("Strongly\nDisagree", "", "", "", "","","Strongly\nAgree"))
p2 = p2 + theme(strip.text.x = element_text(size = 14),
                    strip.background =element_rect(fill="white"),
                    axis.text.x = element_text(face="bold", size=8, 
                                               # angle = 45,
                                               colour = "black"),
                    axis.title.y = element_text(face="bold", size=8))
cat("create Figure 11\n")
p2
cat("end logging")
sink( )

